package com.qfmy.service.impl.common;

import com.qfmy.mapper.common.AddressMapper;
import com.qfmy.service.common.AddressService;
import com.qfmy.service.user.CustomerService;
import common.exception.BaseException;
import common.login.LoginUser;
import common.login.LoginUserHolder;
import common.result.ResultCodeEnum;
import model.entity.common.Address;
import model.query.AddressQuery;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 清风明月
 * @Package com.qfmy.service.impl.common
 * @date 2025/5/18
 * @description 地址表
 */
@Service
@SuppressWarnings("all")
public class AddressServiceImpl implements AddressService {

    /**
     * 注入mapper
     */
    @Autowired
    private AddressMapper addressMapper;
    /**
     * 注入CustomerService
     */
    @Autowired
    private CustomerService customerService;


    /**
     * 添加地址
     * @param addressQuery
     */
    @Override
    public void addAddress(@NotNull AddressQuery addressQuery) {

        // 获取用户id
        Long userId = getUserId();

        // 获取行政编码
        String zipCode = getZipCode(addressQuery.getDistrict());

        // 根据用户id获取地址信息
        Address address = addressMapper.getAddress(userId);
        // 判断是否存在地址信息
        if (address != null) {
            // 设置地址信息
            Address ad = setAddress(addressQuery, userId, zipCode);
            ad.setId(address.getId());
            // 存在则更新
            addressMapper.updateAddress(ad, userId);
        } else {
            address = setAddress(addressQuery, userId, zipCode);
            // 不存在则新增
            addressMapper.addAddress(address, userId);
        }
    }

    /**
     * 获取邮政编码
     * @param district 区域名称
     * @return 邮政编码
     */
    private String getZipCode(String district) {
        String prompt = "请根据我提供的城市的名称" + district + "，请给出该城市的行政编码我只要邮政编码其他的不用回答";
        try {
            return customerService.service(prompt);
        } catch (Exception e) {
            throw new RuntimeException("获取邮政编码失败", e);
        }
    }

    /**
     * 设置地址信息
     *
     * @param addressQuery 地址查询对象
     * @param userId       用户id
     * @param zipCode      邮政编码
     * @param address
     * @return 地址对象
     */
    @NotNull
    private Address setAddress(@NotNull AddressQuery addressQuery, Long userId, String zipCode) {
        Address address = new Address();
        address.setCity(addressQuery.getCity());
        address.setDistrict(addressQuery.getDistrict());
        address.setProvince(addressQuery.getProvince());
        address.setDetailAddress(addressQuery.getDetailAddress());
        address.setPhone(addressQuery.getPhone());
        address.setUsername(addressQuery.getUsername());
        address.setUserId(userId);
        address.setZipCode(zipCode);
        return address;
    }

    /**
     * 获取地址
     * @return
     */
    @Override
    public Address getAddress() {
        //获取当前用户id
        Long userId = getUserId();
        //获取地址
        return addressMapper.getAddress(userId);
    }

    /**
     * 获取用户id
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private Long getUserId() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        return loginUser.getUserId();
    }
}
